package com.pay.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pay.order.entity.ResponseResult;
import com.pay.order.pojo.PayOrder;
import com.pay.order.pojo.User;
import com.pay.order.service.PayOrderService;
import com.pay.order.service.UserService;
import com.pay.order.utils.PageDataResult;
import com.pay.order.utils.SendEmailUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.locks.ReentrantLock;

@Controller
@RequestMapping("/payOrder")
public class PayOrderController {
    @Autowired
    private PayOrderService payOrderService;
    @Autowired
    private UserService userService;
    /**
     * 订单页面
     * @return
     */
    @RequestMapping("/index")
    public String toMemberList() {
        return "order/list";
    }
    @RequestMapping("/create")
    @ResponseBody
    public ResponseResult create(@RequestBody PayOrder payOrder){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        payOrder.setPayeeId(user.getId());
        payOrderService.save(payOrder);
        return  new ResponseResult();
    }
    @RequestMapping("/update/{id}/{status}")
    @ResponseBody
    public ResponseResult update(@PathVariable("id") Integer id,@PathVariable("status") Integer status){
        PayOrder payorder = payOrderService.getById(id);
        payorder.setStatus(status);
        payorder.setUpdateTime(new Date());
        payOrderService.updateById(payorder);
        User user = userService.getById(payorder.getPayeeId());
        SendEmailUtil.sendEmail(user.getEmail(), status==1?"通过":"不通过",2);
        return  new ResponseResult();
    }
    @RequestMapping("/searchList")
    @ResponseBody
    public PageDataResult searchList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, PayOrder payOrder){
        if (null == limit) {
            limit = 10;
        }
        if (null == page) {
            page = 0;
        }else {
            page=(page-1)*limit;
        }
        Page<PayOrder> pageInfo = payOrderService.queryPageList(payOrder,page,limit);
        PageDataResult result = new PageDataResult();
        result.setCode(200);
        result.setTotals((int) pageInfo.getTotal());
        result.setList(pageInfo.getRecords());
        return result;
    }

    @RequestMapping("/get/{id}")
    @ResponseBody
    public ResponseResult<PayOrder> searchList(@PathVariable("id") Integer id){

        PayOrder payOrder = payOrderService.getById(id);
        ResponseResult result = new ResponseResult();
        result.setObj(payOrder);
        return result;
    }


    @RequestMapping("/searchListToWeb")
    @ResponseBody
    public PageDataResult searchList(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        PayOrder payOrder=new PayOrder();
        payOrder.setPayeeId(user.getId());
        Page<PayOrder> pageInfo = payOrderService.queryPageList(payOrder,0,5);
        PageDataResult result = new PageDataResult();
        result.setCode(200);
        result.setTotals((int) pageInfo.getTotal());
        result.setList(pageInfo.getRecords());
        return result;
    }

}
